<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>结构(struct) | haijd</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="c#学习笔记" />
  
  
  
  
  <meta name="description" content="结构(struct)">
<meta name="keywords" content="c#学习笔记">
<meta property="og:type" content="article">
<meta property="og:title" content="结构(struct)">
<meta property="og:url" content="http://www.hais2.com/2015/05/24/c#-struct/index.html">
<meta property="og:site_name" content="haijd">
<meta property="og:description" content="结构(struct)">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp75.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp76.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp77.png">
<meta property="og:updated_time" content="2017-08-02T15:22:33.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="结构(struct)">
<meta name="twitter:description" content="结构(struct)">
<meta name="twitter:image" content="http://www.hais2.com/image/cSharp/cSharp75.png">
  
    <link rel="alternate" href="/atom.xml" title="haijd" type="application/atom+xml">
  
  <link rel="icon" href="/css/images/favicon.ico">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/9749f0/00000000000000000001008f/27/l?subset_id=2&fvd=n5) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/90cf9f/000000000000000000010091/27/l?subset_id=2&fvd=n7) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/8a5494/000000000000000000013365/27/l?subset_id=2&fvd=n4) format("woff2");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/d337d8/000000000000000000010095/27/l?subset_id=2&fvd=i4) format("woff2");font-weight:400;font-style:italic;}</style>
    
  <link rel="stylesheet" id="athemes-headings-fonts-css" href="//fonts.googleapis.com/css?family=Yanone+Kaffeesatz%3A200%2C300%2C400%2C700&amp;ver=4.6.1" type="text/css" media="all">
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css" >
  <link rel="stylesheet" href="/css/hiero.css" >
  <link rel="stylesheet" href="/css/glyphs.css" >
  
    <link rel="stylesheet" href="/css/vdonate.css" >
  

</head>

<script>
var themeMenus = {};

  themeMenus["/"] = "Home"; 

  themeMenus["/archives"] = "Archives"; 

  themeMenus["/categories"] = "Categories"; 

  themeMenus["/tags"] = "Tags"; 

  themeMenus["/about"] = "About"; 

</script>


  <body data-spy="scroll" data-target="#toc" data-offset="50">


  <header id="allheader" class="site-header" role="banner">
  <div class="clearfix container">
      <div class="site-branding">

          <h1 class="site-title">
            
              <a href="/" title="haijd" rel="home"> haijd </a>
            
          </h1>

          
            <div class="site-description">Stay Hungry,Stay Foolish</div>
          
            
          <nav id="main-navigation" class="main-navigation" role="navigation">
            <a class="nav-open">Menu</a>
            <a class="nav-close">Close</a>
            <div class="clearfix sf-menu">

              <ul id="main-nav" class="nmenu sf-js-enabled">
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/">Home</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/archives">Archives</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/categories">Categories</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/tags">Tags</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/about">About</a> </li>
                    
              </ul>
            </div>
          </nav>


      </div>
  </div>
</header>




  <div id="container">
    <div id="wrap">
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-c#-struct" style="width: 66%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
  <div id="articleInner" class="clearfix post-1016 post type-post status-publish format-standard has-post-thumbnail hentry category-template-2 category-uncategorized tag-codex tag-edge-case tag-featured-image tag-image tag-template">
    
    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      结构(struct)
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	Posted on <a href="/2015/05/24/c#-struct/" class="article-date">
	  <time datetime="2015-05-24T15:26:37.000Z" itemprop="datePublished">May 24, 2015</time>
	</a>

      
	<span id="busuanzi_container_page_pv">
	  本文总阅读量<span id="busuanzi_value_page_pv"></span>次
	</span>

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="什么是结构"><a href="#什么是结构" class="headerlink" title="什么是结构"></a>什么是结构</h2><p>　　结构是程序员定义的数据类型，非常类似于类，都有数据成员和函数成员。<br>　　结构就是轻量级的类，使用方式和枚举差不多，方便归类存储数据。<br>　　结构和类的区别是：<br>　　■ 类是引用类型而结构是值类型。<br>　　■ 结构是隐式密封的，这意味着结构不能被派生。<br><strong>声明结构</strong>：<br><figure class="highlight thrift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//声明结构的关键字是“struct”</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">StructName</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    MemberDeclarations</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<a id="more"></a>
<p><strong>使用结构</strong>：<br><figure class="highlight applescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">struct Point</span><br><span class="line"> &#123;</span><br><span class="line">     public int X;</span><br><span class="line">     public int Y;</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"> <span class="built_in">class</span> Program</span><br><span class="line"> &#123;</span><br><span class="line">     static void Main(<span class="built_in">string</span>[] args)</span><br><span class="line">     &#123;</span><br><span class="line">         Point <span class="keyword">first</span>, <span class="keyword">second</span>, <span class="keyword">third</span>;</span><br><span class="line"></span><br><span class="line">         <span class="keyword">first</span>.X = <span class="number">10</span>;</span><br><span class="line">         <span class="keyword">first</span>.Y = <span class="number">10</span>;</span><br><span class="line">         <span class="keyword">second</span>.X = <span class="number">20</span>;</span><br><span class="line">         <span class="keyword">second</span>.Y = <span class="number">20</span>;</span><br><span class="line">         <span class="keyword">third</span>.X = <span class="keyword">first</span>.X + <span class="keyword">second</span>.X;</span><br><span class="line">         <span class="keyword">third</span>.Y = <span class="keyword">first</span>.Y + <span class="keyword">second</span>.Y;</span><br><span class="line"></span><br><span class="line">         Console.WriteLine(<span class="string">"first：&#123;0&#125;——&#123;1&#125;"</span>, <span class="keyword">first</span>.X, <span class="keyword">first</span>.Y);</span><br><span class="line">         Console.WriteLine(<span class="string">"second：&#123;0&#125;——&#123;1&#125;"</span>, <span class="keyword">second</span>.X, <span class="keyword">second</span>.Y);</span><br><span class="line">         Console.WriteLine(<span class="string">"third：&#123;0&#125;——&#123;1&#125;"</span>, <span class="keyword">third</span>.X, <span class="keyword">third</span>.Y);</span><br><span class="line"></span><br><span class="line">         Console.ReadKey();</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果：<br><img src="/image/cSharp/cSharp75.png" alt=""></p>
<h2 id="结构是值类型"><a href="#结构是值类型" class="headerlink" title="结构是值类型"></a>结构是值类型</h2><p>　　和所有的值类型一样，结构类型变量含有它自己的数据(而非引用)，从而：<br>　　■ 结构类型的变量不能为null。<br>　　■ 两个结构变量不能引用(保存、赋值)同一个对象。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CSimple</span> </span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> X;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> Y;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//结构</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Simple</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> X;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> Y;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Program</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span><span class="params">(<span class="built_in">string</span>[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        CSimple cs = <span class="keyword">new</span> CSimple();</span><br><span class="line">        Simple ss = <span class="keyword">new</span> Simple();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>类和结构在内存中的存储方式：<br><img src="/image/cSharp/cSharp76.png" alt=""></p>
<h2 id="对结构赋值"><a href="#对结构赋值" class="headerlink" title="对结构赋值"></a>对结构赋值</h2><p>　　把一个结构赋值给另一个结构，就是从一个结构中把值<strong>复制</strong>到另一个结构。而复制类变量时只有引用被复制了，堆中的数据不变。<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CSimple</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> X;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> Y;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Simple</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> X;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> Y;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Program</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span><span class="params">(<span class="built_in">string</span>[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 类实例</span></span><br><span class="line">        CSimple cs1 = <span class="keyword">new</span> CSimple();</span><br><span class="line">        CSimple cs2 = null;</span><br><span class="line">        <span class="comment">// 结构实例</span></span><br><span class="line">        Simple ss1 = <span class="keyword">new</span> Simple();</span><br><span class="line">        Simple ss2 = <span class="keyword">new</span> Simple();</span><br><span class="line">        </span><br><span class="line">        cs1.X=<span class="number">5</span>;</span><br><span class="line">        ss1.X=<span class="number">5</span>;</span><br><span class="line">        cs1.Y=<span class="number">10</span>;</span><br><span class="line">        ss1.Y=<span class="number">10</span>;</span><br><span class="line">        cs2=cs1;   <span class="comment">//赋值类实例</span></span><br><span class="line">        ss2=ss1;   <span class="comment">//复制结构实例</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>在内存中存储的情况：<br><img src="/image/cSharp/cSharp77.png" alt=""><br>　　■ 类赋值后，<code>cs2</code>和<code>cs1</code>指向堆中的同一对象。<br>　　■ 结构赋值后，<code>ss2</code>中的成员的值和<code>ss1</code>的相同。　</p>
<h2 id="构造函数"><a href="#构造函数" class="headerlink" title="构造函数"></a>构造函数</h2><p>　　<strong>结构可以有实例构造函数和静态构造函数，但是不能有析构函数。</strong></p>
<h3 id="实例构造函数"><a href="#实例构造函数" class="headerlink" title="实例构造函数"></a>实例构造函数</h3><p>　　结构隐式的含有一个无参数的构造函数，这个无参数的构造函数默认的为结构中的每一个成员赋值了默认值(值类型被默认设置成它们的默认值，引用类型默认设置成null)。<br>　　隐式的无参构造函数在每个结构中存在，这个无参构造函数不能被删除，也不能重新定义。<br>　　<strong>结构可以创建另外的含有参数的构造函数</strong>。</p>
<p>　　调用构造函数有两种方法，一种是使用<code>new</code>关键字，一种是不使用<code>new</code>关键字。<br><strong>使用new关键字：</strong><br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Simple</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> X;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> Y;</span><br><span class="line">    <span class="comment">// 带参数的构造函数</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Simple</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        X=a;</span><br><span class="line">        Y=b;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Program</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//调用隐式的无参构造函数</span></span><br><span class="line">        Simple s1 = <span class="keyword">new</span> Simple();</span><br><span class="line">        <span class="comment">//调用定义的有参构造函数</span></span><br><span class="line">        Simple s2 = <span class="keyword">new</span> Simple(<span class="number">5</span>,<span class="number">10</span>);</span><br><span class="line">        </span><br><span class="line">        Console.WriteLine(<span class="string">"&#123;0&#125;,&#123;1&#125;"</span>,s1.X,s1.Y);</span><br><span class="line">        Console.WriteLine(<span class="string">"&#123;0&#125;,&#123;1&#125;"</span>,s2.X,s2.Y);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>不使用new关键字：</strong><br>　　■ 必须给数据成员赋值后才能使用该数据成员。<br>　　■ 必须给所有的数据成员都赋值之后，才能调用函数成员。<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Simple</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> X;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> Y;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Program</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span><span class="params">(<span class="built_in">string</span>[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 不使用new关键字声明</span></span><br><span class="line">        Simple s1,s2;</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//Console.WriteLine("&#123;0&#125;,&#123;1&#125;",s1.X,s1.Y);  //因为没有赋值就使用，编译错误</span></span><br><span class="line">        </span><br><span class="line">        s2.X=<span class="number">5</span>;</span><br><span class="line">        s2.Y=<span class="number">10</span>;</span><br><span class="line">        Console.WriteLine(<span class="string">"&#123;0&#125;,&#123;1&#125;"</span>,s2.X,s2.Y);  <span class="comment">//没有使用new创建实例，但是成员赋值之后是可以使用的。</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="静态构造函数"><a href="#静态构造函数" class="headerlink" title="静态构造函数"></a>静态构造函数</h3><p>　　结构的静态构造函数创建并初始化静态数据成员，而且不能引用实例成员。<br>　　结构的静态构造函数在调用显示声明的构造函数之前被调用。</p>
<h2 id="字段不允许被初始化"><a href="#字段不允许被初始化" class="headerlink" title="字段不允许被初始化"></a>字段不允许被初始化</h2><p>　　<strong>在结构中字段初始化是不允许的。</strong><br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Simple</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="comment">//这种写法是错误的，结构中的字段不允许初始化</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> X = <span class="number">5</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> Y = <span class="number">10</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="结构是密封的"><a href="#结构是密封的" class="headerlink" title="结构是密封的"></a>结构是密封的</h2><p>　　结构总是密封的，所以，不能从结构派生其他的结构。<br>　　由于结构不支持继承，所以下列类修饰符不能用于结构：<br>　　● protected<br>　　● internal<br>　　● abstract<br>　　● virtual<br>　　<strong>结构派生自System.ValueType类</strong></p>
<h2 id="结构作为返回类型和参数"><a href="#结构作为返回类型和参数" class="headerlink" title="结构作为返回类型和参数"></a>结构作为返回类型和参数</h2><p>　　结构可以用作返回值和参数。<br>　　■ <strong>返回值</strong>：当结构被当做返回值时，一个该结构的拷贝被创建并从函数成员返回。<br>　　■ <strong>值参</strong>：当结构被当做值参时，一个实际参数的拷贝被创建，该拷贝用在方法的执行中。<br>　　■ <strong>ref和out参数</strong>：如果把一个结构用作ref或者out参数，一个对该结构的引用被传入方法，这样其数据成员就能被改变。</p>
<h2 id="关于结构"><a href="#关于结构" class="headerlink" title="关于结构"></a>关于结构</h2><p>　　分配结构比创建类的实例需要更少的消耗，所以使用结构代替类有时可以提高性能，但是结构是值类型，如果想使用一个结构实例作为引用类型的对象，必须执行拆装箱操作，代价比较高。<br>　　■ 预定义简单类型(int、short、long等等),尽管在.NET和c#中被视为原始类型，但是它们在.NET中都被实现为结构。<br>　　■ 可以使用声明partial类相同的方法声明partial结构。<br>　　■ 结构可以实现接口。</p>

      
    </div>
    <footer class="entry-meta entry-footer">
      
	<span class="ico-folder"></span>
    <a class="article-category-link" href="/categories/c/">c#</a>

      
  <span class="ico-tags"></span>
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/c-学习笔记/">c#学习笔记</a></li></ul>

      
        <div id="donation_div"></div>

<script src="/js/vdonate.js"></script>
<script>
var a = new Donate({
  title: '如果觉得我的文章对您有用，请随意打赏。您的支持将鼓励我继续创作!', // 可选参数，打赏标题
  btnText: 'Donate', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: '/css/images/wechatPay.png',
  alipayImage: '/css/images/alipay.jpg'
});
</script>
      

      
        
	<section id="comments" class="comment">
	  <div id="disqus_thread">
	  <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
	  </div>
	</section>

	<script type="text/javascript">
	var disqus_shortname = 'haijdblog';
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/count.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	</script>


      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2015/06/03/c#-interface/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          接口(interface)
        
      </div>
    </a>
  
  
    <a href="/2015/05/22/c#-generic/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">泛型(generic)</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">Contents</strong>
    
      <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#什么是结构"><span class="nav-number">1.</span> <span class="nav-text">什么是结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结构是值类型"><span class="nav-number">2.</span> <span class="nav-text">结构是值类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#对结构赋值"><span class="nav-number">3.</span> <span class="nav-text">对结构赋值</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#构造函数"><span class="nav-number">4.</span> <span class="nav-text">构造函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#实例构造函数"><span class="nav-number">4.1.</span> <span class="nav-text">实例构造函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#静态构造函数"><span class="nav-number">4.2.</span> <span class="nav-text">静态构造函数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#字段不允许被初始化"><span class="nav-number">5.</span> <span class="nav-text">字段不允许被初始化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结构是密封的"><span class="nav-number">6.</span> <span class="nav-text">结构是密封的</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结构作为返回类型和参数"><span class="nav-number">7.</span> <span class="nav-text">结构作为返回类型和参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#关于结构"><span class="nav-number">8.</span> <span class="nav-text">关于结构</span></a></li></ol>
    
    </div>
  </aside>
</section>
        
      </div>
      <footer id="footer" class="site-footer">
  

  <div class="clearfix container">
      <div class="site-info">
	      &copy; 2018 haijd All Rights Reserved.
          
            <span id="busuanzi_container_site_uv">
              本站访客数<span id="busuanzi_value_site_uv"></span>人次  
              本站总访问量<span id="busuanzi_value_site_pv"></span>次
            </span>
          
      </div>
      <div class="site-credit">
        Theme by <a href="https://github.com/iTimeTraveler/hexo-theme-hiero" target="_blank">hiero</a>
      </div>
  </div>
</footer>


<!-- min height -->

<script>
    var contentdiv = document.getElementById("content");

    contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("allheader").offsetHeight - document.getElementById("footer").offsetHeight + "px";
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>
<script src="/js/bootstrap.js"></script>
<script src="/js/main.js"></script>








	<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js" async=""></script>
</body>
</html>
